package com.baizhi.Controller;
import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.annotation.DeleteCacheAnnotation;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.service.BannerService;
import com.baizhi.entity.Banner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @LogAnnotation(value = "添加轮播图" ,type="insert")
    @DeleteCacheAnnotation
    @RequestMapping("insert")
    public @ResponseBody Map insert(MultipartFile file, Banner banner,HttpServletRequest request) throws IOException {
        //获得绝对路径
        String realPath=request.getServletContext().getRealPath("/img");
        System.out.println(realPath+"===========绝对路径");
        //通过uuid获得新名字、
        if(file!=null) {
            String newFileName = new Date().getTime() + "_" + file.getOriginalFilename();
            System.out.println(newFileName + "========文件名字");
            file.transferTo(new File(realPath + "/" + newFileName));
            banner.setBannerImageUrl("/img/" + newFileName);
        }
        Map map=bannerService.insertBanner(banner);
        return  map;
    }
    @LogAnnotation(value = "删除轮播图" ,type="delete")
    @DeleteCacheAnnotation
    @RequestMapping("delete")
    public @ResponseBody Map delete(int bannerId){
        Map map=bannerService.deleteBannerById(bannerId);
        return map;
    }
    @LogAnnotation(value = "更新轮播图" ,type="update")
    @DeleteCacheAnnotation
    @RequestMapping("update")
    public @ResponseBody Map update(MultipartFile file, Banner banner,HttpServletRequest request) throws IOException {
        //获得绝对路径
        String realPath=request.getServletContext().getRealPath("/img");
        System.out.println(realPath+"===========绝对路径");
      //通过uuid获得新名字、
        if(file!=null){
            String newFileName = new Date().getTime() + "_" + file.getOriginalFilename();
            file.transferTo(new File(realPath + "/" + newFileName));
            banner.setBannerImageUrl("/img/" + newFileName);
        }
        Map map=bannerService.updateBanner(banner);
        return  map;
    }
    @LogAnnotation(value = "查询轮播图" ,type="select")
    @AddCacheAnnotation
    @RequestMapping("showAll")
    public @ResponseBody Map showAll(int page,int limit){
        Map map=bannerService.showAllBanner(page,limit);
        return map;
    }
    @RequestMapping("selectOne")
    @AddCacheAnnotation
    public @ResponseBody Banner selectOne(int bannerId){
        System.out.println(bannerId+"================ID");
        Banner banner=bannerService.selectOne(bannerId);
        return banner;
    }
}
